package com.sit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sit.entity.*;
import com.sit.mapper.CompetitionTMapper;
import com.sit.mapper.CtMapper;
import com.sit.mapper.TeamMapper;
import com.sit.service.CtService;
import com.sit.vo.CtVO;
import com.sit.vo.DataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CtServiceImpl implements CtService {


    @Autowired
    private CtMapper ctMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private CompetitionTMapper competitionTMapper;

        @Override
        public DataVO<CtVO> findData(Integer page, Integer limit){
            DataVO dataVO = new DataVO();
            dataVO.setCode(0);
            dataVO.setMsg("");

            //分页查询
            //new一个page对象，用于封装page信息
            IPage<Ct> ctIPage = new Page<>(page, limit);
            //通过查询再返回一个page对象
            IPage<Ct> result = ctMapper.selectPage(ctIPage, null);

            dataVO.setCount(result.getTotal());

            //List<Ca> caList = caMapper.selectList(null);
            //分页page对象不仅包含所有数据，还包含分页信息，所以直接通过result就能拿到所有记录
            List<Ct> ctList = result.getRecords();
            List<CtVO> ctVOList = new ArrayList<>();
            for (Ct ct : ctList) {
                CtVO ctVO = new CtVO();
                BeanUtils.copyProperties(ct, ctVO);
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("tno", ct.getTno());
                ctVO.setTn(teamMapper.selectOne(wrapper).getTn());

                wrapper = new QueryWrapper();
                wrapper.eq("tno", ct.getTno());
                ctVO.setMonitor(teamMapper.selectOne(wrapper).getMonitor());

                wrapper = new QueryWrapper();
                wrapper.eq("cno_a", ct.getCnoA());
                ctVO.setCnA(competitionTMapper.selectOne(wrapper).getCnA());

                wrapper = new QueryWrapper();
                wrapper.eq("cno_a", ct.getCnoA());
                ctVO.setPlaceA(competitionTMapper.selectOne(wrapper).getPlaceA());

                wrapper = new QueryWrapper();
                wrapper.eq("cno_a", ct.getCnoA());
                ctVO.setDateA(competitionTMapper.selectOne(wrapper).getDateA());
                ctVOList.add(ctVO);
            }
            dataVO.setData(ctVOList);
            return dataVO;
    }

    @Override
    public boolean addTeam(AddTeam addTeam) {
        Team team = new Team();
        team.setTno(addTeam.getTno());
        team.setTn(addTeam.getTn());
        team.setMonitor(addTeam.getMonitor());
        int flag1 =teamMapper.insert(team);

        Ct ct = new Ct();
        ct.setTno(addTeam.getTno());
        ct.setCnoA(addTeam.getCnoA());
        int flag2 = ctMapper.insert(ct);
        if(flag1 >0 && flag2 > 0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean updateTeam(AddTeam addTeam,String cnoA_new) {
        Team team = new Team();
        team.setTn(addTeam.getTn());
        team.setTno(addTeam.getTno());
        team.setMonitor(addTeam.getMonitor());
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("tno",team.getTno());
        int flag =teamMapper.update(team,wrapper);

        Ct ct = new Ct();
        ct.setTno(addTeam.getTno());
        ct.setCnoA(addTeam.getCnoA());
        Map<String, Object> map = new HashMap<>();
        map.put("tno",ct.getTno());
        map.put("cno_a", ct.getCnoA());
        int flag1 = ctMapper.deleteByMap(map);

        ct.setCnoA(cnoA_new);
        int flag2 =ctMapper.insert(ct);
        if(flag >0 &&flag1 >0 && flag2 > 0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean deleteTeam(String tno, String cnoA) {
        Map<String, Object> map = new HashMap<>();
        map.put("tno", tno);
        map.put("cno_a", cnoA);
        int flag = ctMapper.deleteByMap(map);
        if (flag>0){
            return true;
        }else
            return false;
    }

    @Override
    public DataVO<CtVO> search(String tno, String cnA, Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        //分页查询
        //new一个page对象，用于封装page信息
        IPage<Ct> caIPage = new Page<>(page,limit);
        //通过查询再返回一个page对象
        QueryWrapper wrapper = new QueryWrapper();

        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("cn_a",cnA);
        String cnoA = competitionTMapper.selectOne(wrapper2).getCnoA();

        Map<String, Object> map = new HashMap<>();
        map.put("tno", tno);
        map.put("cno_a", cnoA);
        wrapper.allEq(map);
        IPage<Ct> result = ctMapper.selectPage(caIPage,wrapper);

        dataVO.setCount(result.getTotal());

        //List<Ca> caList = caMapper.selectList(null);
        //分页page对象不仅包含所有数据，还包含分页信息，所以直接通过result就能拿到所有记录
        List<Ct> ctList = result.getRecords();
        List<CtVO> ctVOList = new ArrayList<>();
        for (Ct ct : ctList) {
            CtVO ctVO = new CtVO();
            BeanUtils.copyProperties(ct,ctVO);
            QueryWrapper wrapper3 = new QueryWrapper();
            wrapper3.eq("tno",ct.getTno());
            ctVO.setTn(teamMapper.selectOne(wrapper3).getTn());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("tno",ct.getTno());
            ctVO.setMonitor(teamMapper.selectOne(wrapper3).getMonitor());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ct.getCnoA());
            ctVO.setCnA(competitionTMapper.selectOne(wrapper3).getCnA());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ct.getCnoA());
            ctVO.setPlaceA(competitionTMapper.selectOne(wrapper3).getPlaceA());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ct.getCnoA());
            ctVO.setDateA(competitionTMapper.selectOne(wrapper3).getDateA());
            ctVOList.add(ctVO);
        }
        dataVO.setData(ctVOList);
        return dataVO;
    }

    @Override
    public boolean editScoreT(String tno, String cnoA, Integer rank, String score) {
        Ct ct = new Ct();
        ct.setTno(tno);
        ct.setCnoA(cnoA);
        Map<String, Object> map = new HashMap<>();
        map.put("tno",ct.getTno());
        map.put("cno_a", ct.getCnoA());
        int flag1 = ctMapper.deleteByMap(map);

        ct.setRank(rank);
        ct.setScore(score);
        int flag2 =ctMapper.insert(ct);
        if(flag1 >0 && flag2 > 0){
            return  true;
        }else {return false;}
    }
}
